package com.cartrentalsystem.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cartrentalsystem.domain.po.Car;
import com.cartrentalsystem.domain.po.Customers;
import com.cartrentalsystem.domain.vo.Result;
import com.cartrentalsystem.service.ICarService;
import com.cartrentalsystem.service.IRepairService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.time.LocalDate;
import java.util.Date;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LHD
 * @since 2025-06-15
 */
@RestController
@RequestMapping("/car")
@RequiredArgsConstructor
@Slf4j
public class CarController {

    private final ICarService carService;

    private final IRepairService  repairService;


    @GetMapping("/list")
    public Result<Page<Car>> list(@Param("page") Integer page, @Param("size") Integer size,  @Param("status") Integer status,@Param("brand") String brand) {
        log.info("page:{},size:{},status:{},brand:{}",  page, size, status,brand);
        return carService.pageList(page, size,status,brand);
    }

    @PostMapping("/add")
    public Result<String> add(@RequestBody Car car) {
        return carService.add(car);
    }

    @PutMapping("/update")
    public Result<String> updateCar(@RequestBody Car car) {
        return carService.updateCar(car);
    }

    @DeleteMapping("/delete/{id}")
    public Result<String> delete(@PathVariable Integer id) {
        boolean b = carService.removeById(id);
        return b ? Result.success("删除成功") : Result.fail("删除失败");
    }

    /**
     * 更新车辆状态
     */
    @PutMapping("/updateStatus/{id}")
    public Result<String> updateStatus(@PathVariable Integer id
            ,@RequestParam("status") Integer status
            ,@RequestParam(name = "mileage",defaultValue = "0") Double mileage
            ,@RequestParam("returnTime") LocalDate returnTime
            ,@RequestParam(required = false,name = "repairReason") String repairReason
            ,@RequestParam(required = false,name = "repairMoney") Double repairMoney
            ,@RequestParam(required = false,name = "employeeName") String employeeName
    ) {
        System.out.println("status:"+status);
        if (status == 2){
            boolean  b = carService.updateStatus(id,status,mileage,returnTime);
            return b ? Result.success("更新成功") : Result.fail("更新失败");
        }
        else {
            boolean b = carService.updateStatus(id,status,mileage,returnTime);
            boolean b1 = repairService.insertRepair(id,repairReason,repairMoney,employeeName);
            return b && b1 ? Result.success("更新成功") : Result.fail("更新失败");
        }
    }

    @GetMapping("/names")
    public Result<String[]> getNames() {
        String[] names = carService.getNames();
        return Result.success(names);
    }

    /**
     * 获取可用车辆列表
     */
    @GetMapping("/available")
    public Result<Page<Car>> getAvailable(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                          @RequestParam(value = "size",defaultValue = "10") Integer size) {
        log.info("page:{},size:{}",  page, size);
        return carService.getAvailable(page, size);
    }
}
